from flask import Blueprint, jsonify
from ..database import db

bp = Blueprint('show', __name__, url_prefix='/show')


# a simple page that says hello
@bp.route('/cards', methods=['GET'])
def show():
    ret = {'cards': [
        {'name': "name",
         'content': [
             {'title':  "百度",
              'url': "www.baidu.com"},
         ]},
    ]}
    db.reflect()     #1、映射app数据库中的表（app其实就是本程序的flask实例，已连接到数据库）

    tables = db.metadata.tables  #2、取得所有数据库（返回：immutabledict，里面实际包含了数据库中所有表的结构

    print(tables)
    cards = []
    for (k, v) in tables.items():
        a = db.session.query(v).all()
        content = [x._asdict() for x in a]
        cards.append({
            'name': k, 'content': content})
    return jsonify(ret)
    #return jsonify({'cards': cards})

